package com.example.book_shop_java.controller;

import com.alibaba.fastjson.JSON;
import com.example.book_shop_java.bean.Car;
import com.example.book_shop_java.dao.CarDao;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

@RestController
public class CarController {
    @Resource
    CarDao carDao;

    @CrossOrigin
    @RequestMapping("/getAllCar")
    public String getAllCar(int wx_userid){
        List<Car> cars = carDao.getAllCar(wx_userid);
        String message = "error";
        int code = 413;
        if(cars.size() > 0){
            code = 200;
            message = "success";
        }
        HashMap<String, Object> res = new HashMap<>();
        res.put("message",message);
        res.put("code",code);
        res.put("data",cars);
        String allCarMessage = JSON.toJSONString(res);
        return allCarMessage;
    }

    @RequestMapping("/plusNum")
    public String plusNum(int car_id){
        int i = carDao.plusNum(car_id);
        int code = 413;
        HashMap<String, Object> res = new HashMap<>();
        String message = "error";
        if(i > 0){
            code = 200;
            message = "success";
        }
        res.put("message",message);
        res.put("code",code);
        String plusMessage = JSON.toJSONString(res);
        return plusMessage;
    }

    @RequestMapping("/reduceNum")
    public String reduceNum(int car_id){
        int i = carDao.reduceNum(car_id);
        int code = 413;
        HashMap<String, Object> res = new HashMap<>();
        String message = "error";
        if(i > 0){
            code = 200;
            message = "success";
        }
        res.put("message",message);
        res.put("code",code);
        String reduceMessage = JSON.toJSONString(res);
        return reduceMessage;
    }

    @RequestMapping("/deleteCar")
    public String deleteCar(int car_id){
        int i = carDao.deleteCar(car_id);
        int code = 413;
        HashMap<String, Object> res = new HashMap<>();
        String message = "error";
        if(i > 0){
            code = 200;
            message = "success";
        }
        res.put("message",message);
        res.put("code",code);
        String deleteMessage = JSON.toJSONString(res);
        return deleteMessage;
    }

    @RequestMapping("/updateCarState")
    public String updateCarState(int car_id,
                                 @RequestParam("state") Boolean state
    ){
        int i = carDao.updateCarState(car_id,state);
        String str = i >0?"success":"error";
        return str;
    }

    @RequestMapping("/updateAllCarState")
    public String updateAllCarState(int wx_userid){
        carDao.updateCheckState();
        int check_state = carDao.getCheckState();
        int i = carDao.updateAllCarState(check_state,wx_userid);
        int code = 413;
        HashMap<String, Object> res = new HashMap<>();
        String message = "error";
        if(i > 0){
            code = 200;
            message = "success";
        }
        res.put("message",message);
        res.put("code",code);
        String updateMessage = JSON.toJSONString(res);
        return updateMessage;
    }

    @RequestMapping("/getCheckedGood")
    public String getCheckedGood(int wx_userid){
        List<Car> cars = carDao.getCheckedGood(wx_userid);
        String message = "error";
        int code = 413;
        if(cars.size() > 0){
            code = 200;
            message = "success";
        }
        HashMap<String, Object> res = new HashMap<>();
        res.put("message",message);
        res.put("code",code);
        res.put("data",cars);
        String allCheckedCarMessage = JSON.toJSONString(res);
        return allCheckedCarMessage;
    }

    @RequestMapping("/addOrder")
    public String addOrder(int wx_userid,
                           int car_id,
                           @RequestParam("address") String address){
        List<Car> car = carDao.getCarGood(car_id);
        String goodImg = car.get(0).getGoodImg();
        String goodName = car.get(0).getGoodName();
        int goodNum = car.get(0).getGoodNum();
        String goodPrice = car.get(0).getGoodPrice();
        LocalDateTime localDateTime = LocalDateTime.now();
        String timeOut = JSON.toJSONString(localDateTime);
        int i = carDao.addOrder(wx_userid,goodImg,address,timeOut,goodName,goodNum,goodPrice);
        String message = "error";
        carDao.deleteCar(car_id);
        int code = 413;
        if(car.size() > 0){
            code = 200;
            message = "success";
        }
        HashMap<String, Object> res = new HashMap<>();
        res.put("message",message);
        res.put("code",code);
        String allCheckedCarMessage = JSON.toJSONString(res);
        return allCheckedCarMessage;
    }
}
